package com.zh.template;


import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.harmony.HarmonyDTO;
import com.getui.push.v2.sdk.dto.req.message.harmony.HarmonyNotification;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.zh.properties.GeTuiProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.Message;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取推送模板
 */
@Slf4j
public class GetTuiTemplate {
    private GeTuiProperties geTuiProperties;
    public GetTuiTemplate(GeTuiProperties geTuiProperties){
        this.geTuiProperties = geTuiProperties;
    }

    /**
     * 创建用户Api （用户Api： 可用于绑定别名）
     * @return
     */
    public UserApi createUserApi() {
        ApiHelper apiHelper = getApiHelper();
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        UserApi userApi = apiHelper.creatApi(UserApi.class);
        return userApi;
    }

    /**
     * 创建推送API（推送API： 可用于别名推送、cid推送、等）
     * @return
     */
    public PushApi createPusApi() {
        ApiHelper apiHelper = getApiHelper();
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        return pushApi;
    }


    /**
     * 根据别名推送 消息
     * @param alias 别名（这里用乘客ID 作为别名）
     */
    public void pushToSingleByAlias(String alias, String message) {
        PushDTO<Audience> pushDTO = pushDTO(message);
        fullAlias(pushDTO,alias);
        ApiResult<Map<String, Map<String, String>>> apiResult = createPusApi().pushToSingleByAlias(pushDTO);
        log.info("推送结果：{}", apiResult);
    }


    /**
     * 创建推送Api模板
     * @return
     */
    private ApiHelper getApiHelper() {
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(geTuiProperties.getAppId());
        apiConfiguration.setAppKey(geTuiProperties.getAppKey());
        apiConfiguration.setMasterSecret(geTuiProperties.getMasterSecret());
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀
        apiConfiguration.setDomain(geTuiProperties.getDomain());
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        return apiHelper;
    }

    /**
     * 构建完整别名
     * @param pushDTO
     * @param alias  别名（这里用乘客ID 作为别名）
     */
    private void fullAlias(PushDTO<Audience> pushDTO,String alias) {
        Audience audience = new Audience();
        audience.addAlias(alias);
        pushDTO.setAudience(audience);
    }

    /**
     * 创建推送DTO
     * @return
     */
    private PushDTO<Audience> pushDTO(String message) {
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("g-name1");

        Settings settings = new Settings();
        settings.setTtl(3600000);
        pushDTO.setSettings(settings);
        //构建消息
        PushMessage pushMessage = new PushMessage();
        // 设置透传消息 （消息格式可自定义， 如纯文本、json串），传递给前端由onPushMsg()接收 推送消息回调 注意， 该字段为透传消息时生效
        pushMessage.setTransmission(message);
        pushDTO.setPushMessage(pushMessage);
        return pushDTO;
    }
}
